<!--
 * @Descripttion: 代理商设置
 * @Author: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @LastEditTime: 2023-08-01 16:42:08
-->
<template>
  <div class="lb-system-wechat">
    <top-nav></top-nav>
    <div class="page-main">
      <el-form
        @submit.native.prevent
        :model="subForm"
        :rules="subFormRules"
        ref="subForm"
        label-width="180px"
        class="config-form"
      >
        <el-form-item
          :label="'绑定' + $t('action.attendantName') + '背景图'"
          prop="bind_technician_img"
        >
          <lb-cover
            :fileList="subForm.bind_technician_img"
            @selectedFiles="getCover($event, 'bind_technician_img')"
          ></lb-cover>
          <lb-tool-tips
            >邀请{{ $t('action.attendantName') }}推广图，图片建议尺寸：710 *
            1138
            <div class="mt-sm">
              代理商邀请{{ $t('action.attendantName') }}海报，通过该海报可以将{{
                $t('action.attendantName')
              }}绑定在自己的账号下获得佣金
            </div>
            <div class="mt-sm">
              由于页面生成的二维码位置是固定的，所以设计海报时注意将中间的二维码位置留出来
            </div>
            <div class="mt-sm">
              海报背景图中不要出现诱导用户分享以及传播外链内容的
            </div>
            <div class="mt-sm">
              包括但不限于：以金钱奖励、实物奖励、虚拟奖品（包括但不限于红包、优惠券、代金券、积分、话费、流量）；声称分享可获得返佣等
            </div>
          </lb-tool-tips>
          <div>
            <lb-button
              type="danger"
              plain
              size="small"
              @click="toReset('bind_technician_img')"
              >{{ $t('action.defaultSet') }}</lb-button
            >
          </div>
        </el-form-item>
        <el-form-item label="商家信息" prop="merchant_switch_show">
          <el-radio-group v-model="subForm.merchant_switch_show">
            <el-radio :label="1">{{ $t('action.ON') }}</el-radio>
            <el-radio :label="0">{{ $t('action.OFF') }}</el-radio>
          </el-radio-group>
          <lb-tool-tips
            >当选择开启时，若{{
              $t('action.attendantName')
            }}有关联的代理商且代理商有商家名称，则{{
              $t('action.attendantName')
            }}列表页、详情页将展示商家信息</lb-tool-tips
          >
        </el-form-item>
        <el-form-item>
          <lb-button type="primary" @click="submitFormInfo" v-preventReClick>{{
            $t('action.submit')
          }}</lb-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  data () {
    return {
      bind_technician_img: 'https://lbqnyv2.migugu.com/bianzu18.png',
      subForm: {
        bind_technician_img: [],
        merchant_switch_show: 0
      },
      subFormRules: {
        bind_technician_img: {
          required: true, type: 'array', message: '请选择绑定' + this.$t('action.attendantName') + '背景图', trigger: 'blur'
        },
        merchant_switch_show: { required: true, type: 'number', message: '请选择', trigger: 'blur' }
      }
    }
  },
  created () {
    this.getFormInfo()
  },
  methods: {
    async getFormInfo () {
      let { code, data } = await this.$api.system.configInfo()
      if (code !== 200) return
      data.bind_technician_img = data.bind_technician_img && data.bind_technician_img.length > 0 ? [{ url: data.bind_technician_img }] : []
      for (let i in this.subForm) {
        this.subForm[i] = data[i]
      }
    },
    getCover (img, key) {
      this.subForm[key] = img
    },
    toReset (key) {
      this.subForm[key] = [{ url: this[key] }]
      this.submitFormInfo()
    },
    async submitFormInfo () {
      let flag = false
      this.$refs['subForm'].validate(valid => {
        if (valid) {
          flag = true
        }
      })
      let subForm = JSON.parse(JSON.stringify(this.subForm))
      subForm.bind_technician_img = subForm.bind_technician_img[0].url
      if (flag) {
        await this.$api.system.configUpdate(subForm)
        this.$message.success(this.$t('tips.successSub'))
      }
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-system-wechat {
  width: 100%;
  .config-form {
    .el-input {
      width: 300px;
    }
  }
}
</style>
